/*
 * $Id$
 */

package corejs.rt;

import java.math.BigDecimal;

/**
 *
 * @author pengyu
 */
public class Long extends Number {
   
   private long _val;

   public static Long valueOf(final long l) {
      return new Long(l);
   }

   public Long(final long l) {
      _val = l;
   }

   // ---------------------------------------
   // add
   @Override
   public Number add(final Number n) { return n.add(_val); }

   @Override
   protected Number add(final int i) { return valueOf(_val + i); }

   @Override
   protected Number add(final long l) { return valueOf(_val + l); }

   @Override
   protected Number add(final BigDecimal d) {
      return new Decimal(d.add(new BigDecimal(_val)));
   }

   // ---------------------------------------
   // multiply
   @Override
   public Number multiply(final Number n) { return n.multiply(_val); }

   @Override
   protected Number multiply(final int i) { return valueOf(_val * i); }

   @Override
   protected Number multiply(final long l) { return valueOf(_val * l); }

   @Override
   protected Number multiply(final BigDecimal d) {
      return new Decimal(d.multiply(new BigDecimal(_val)));
   }

   // ---------------------------------------
   // subtract
   @Override
   public Number subtract(final Number n) {
      return n.subtract(_val);
   }

   @Override
   protected Number subtract(final long l) { return valueOf(l - _val); }

   @Override
   protected Number subtract(final int i) { return valueOf(i - _val); }

   @Override
   protected Number subtract(final BigDecimal d) {
      return new Decimal(d.subtract(new BigDecimal(_val)));
   }

   // ---------------------------------------
   // equals
   @Override
   public boolean equals(final Number y) {return y.equals(_val);}
   
   @Override
   public boolean equals(final long l) { return _val == l; }

   @Override
   public boolean equals(final int i) { return _val == i; }

   @Override
   public boolean equals(final BigDecimal d) {
      return d.equals(new BigDecimal(_val));
   }

   // ---------------------------------------
   // compareTo
   @Override
   public Number compareTo(final Number other) { return other.compareTo(_val); }

   @Override
   Number compareTo(final int i) { return compareTo((long)i); }

   @Override
   Number compareTo(final long l) {
      return l == _val ? Int.ZERO
              : l < _val ? Int.ONE
              :            Int.MINUS_ONE;
   }

   @Override
   Number compareTo(final BigDecimal d) {
      final int i = d.compareTo(new BigDecimal(_val));
      return i == 0 ? Int.ZERO
              : i < 0 ? Int.MINUS_ONE
              :         Int.ONE;
   }

   @Override
   public Bool toBool() { return 0 == _val ? Bool.FALSE : Bool.TRUE; }

   @Override
   public Str toStr() { return new Str(_val); }

   @Override public int intValue() { return (int)_val; }

   @Override public long longValue() { return _val; }
  
   @Override public Long neg() {return valueOf(-_val);}
}
